unit FeedingCategoryRecord;

{$mode objfpc}{$H+}

//========================================================================================
//
// Unit : FeedingCategoryRecord.pas
//
// Description :
//
// Calls : HUtils : ValidAlphaCharacter
//
// Called By : FeedingCategoryTable : pumAddCategoryClick
//                                    pumEditCategoryClick
//                                    pumInsertCategoryClick
//                                    pumDeleteCategoryClick
//
// Ver. : 1.0.0
//
// Date : 30 Sep 2015
//
//========================================================================================

interface

uses
  Classes, dB, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  StdCtrls, ExtCtrls,
  // Application  units
  HUtils;

type

  TFeedingCategoryRecord = record
    CategoryID: integer;
    Category: string;
    ActiveState: boolean;
  end;// TFeedingCategopryRecord = record

  { TfrmFeedingCategoryRecord }

  TfrmFeedingCategoryRecord = class(TForm)
    bbtCancel: TBitBtn;
    bbtOK: TBitBtn;
    bbtHelp: TBitBtn;
    chkActive: TCheckBox;
    edtCategory: TEdit;
    edtCategoryID: TEdit;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    stxFunctionMessage: TStaticText;
    procedure bbtCancelClick(Sender: TObject);
    procedure bbtHelpClick(Sender: TObject);
    procedure bbtOKClick(Sender: TObject);
    procedure edtCategoryKeyPress(Sender: TObject; var Key: char);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    function ProcessRecord( TableState: TDataSetState;
                         var FeedingCategoryRec: TFeedingCategoryRecord) : boolean;
  end;// TfrmFeedingCategoryRecord = class(TForm)

var
  frmFeedingCategoryRecord: TfrmFeedingCategoryRecord;

implementation

{$R *.lfm}

uses
  FeedingCategoryTable;

const
  cstrAddMessage = 'Adding a New Category consists of entering a Category' +
                   'name and selecting or deselecting the Active checkbox.' +
                   'The Categpry name must contain at least three, and no more' +
                   ' than 15, alphabetic characters from {A..Z}, [a,,z] or a <Space>.' +
                   'The new Categpry must be unique, not already in the Categpry table.' +
                   #13+#13+
                   'If the new Category does not meet these conditions, when the ' +
                   '<OK> button is selected, an error message will be displayed and' +
                   'the cursor will be reset to the Category edit box.' +
                   #13+#13+
                   'The Activecheck box will determine if the new Category will be' +
                   'available for use or not. You can use this capability to set up' +
                   'a new Categpry to be used at a future date by deselectring the' +
                   'Active checkbox now and then editing the record and selecting it' +
                   'in the future to enable it.' +
                   #13+#13+
                   'At any time you can select the <Cancel> button to cancel the' +
                   'process and return to the application with no addtional record.';

  cstrCancellationMessage = 'Confirm you wish to Cancel this New Category.' +
                            #13 +
                            'It will not be added to the Category table.';
var
  vrecNewCategoryRecord : TFeedingCategoryRecord;
  vintCategoryID : Integer;
  vstrCCategory : Integer;

//========================================================================================
//          SUPPORT ROUTINES
//========================================================================================

//========================================================================================
//          PRIVATE ROUTINES
//========================================================================================

//========================================================================================
//          PUBLIC ROUTINES
//========================================================================================
function TfrmFeedingCategoryRecord.ProcessRecord ( TableState: TDataSetState;
                            var FeedingCategoryRec: TFeedingCategoryRecord ) : Boolean;
begin

  //Save the Working Record
  vrecNewCategoryRecord := FeedingCategoryRec;

  case TableState of
    dsInsert : begin
                 frmFeedingCategoryRecord.Caption := 'Adding Rceord';
                 frmFeedingCategoryRecord.stxFunctionMessage.Caption := cstrAddMessage;
                 edtCategoryID.Text := IntToStr(FeedingCategoryRec.CategoryID);
                 edtCategory.Text := FeedingCategoryRec.Category;
                 chkActive.Checked := FeedingCategoryRec.ActiveState;
               end;
    dsEdit : begin
                 edtCategoryID.Text := IntToStr(FeedingCategoryRec.CategoryID);
                 edtCategory.Text := FeedingCategoryRec.Category;
                 chkActive.Checked := FeedingCategoryRec.ActiveState;
               end;
  end;// case TableState of

  frmFeedingCategoryRecord.ShowModal;

  if ModalResult = mrOK then
  begin
    FeedingCategoryRec := vrecNewCategoryRecord;
    Result := True;
  end
  else
    Result := False;

end;// function TfrmFeedingCategoryRecord.ProcessRecord(TableState: TDataSetState)

//========================================================================================
//          PUBLISHED ROUTINES
//========================================================================================

//========================================================================================
//          PROPERTY ROUTINES
//========================================================================================

//========================================================================================
//          MENU ROUTINES
//========================================================================================

//========== POPUP MENUS ===================================================================

//========================================================================================
//          TOOLBAR ROUTINES
//========================================================================================

//========================================================================================
//           STATUSBAR ROUTINES
//========================================================================================

//========================================================================================
//          FILE ROUTINES
//========================================================================================

//========================================================================================
//          COMMAND BUTTON ROUTINES
//========================================================================================
procedure TfrmFeedingCategoryRecord.bbtOKClick(Sender: TObject);
begin

  // First we validate the Category
  if Length(edtCategory.Text) < cbytMinFeedingCategoryLength then
  begin
    showmessage('Too Short');
    ModalResult := mrNone;
    edtCategory.SetFocus;
    Exit;
  end;

  // Now we make sure that the Category doesnt exist
  if frmFeedingCategoryTable.dbfFeedingCategoryTable.Locate
               (cstrFeedingCategory, edtCategory.Text, []) then
  begin
    showmessage('Duplicate Category');
    edtCategory.SetFocus;
    ModalResult := mrNone;
    Exit;
  end;// if frmFeedingCategoryTable.dbfFeedingCategoryTable.Locate

  // Now we set the Categpory and Active State fields into the Original Record
  vrecNewCategoryRecord.CategoryID := StrToInt(edtCategoryID.Text);
  vrecNewCategoryRecord.Category := edtCategory.Text;
  vrecNewCategoryRecord.ActiveState := chkActive.Checked;

end;// procedure TfrmFeedingCategoryRecord.bbtOKClick

//========================================================================================
procedure TfrmFeedingCategoryRecord.bbtCancelClick(Sender: TObject);
begin

  // Confirm the Cnacellation. If it is confirmed we return the Original record
  // unchanged
  if ConfirmationMessageDlg('Confirm Cancellation', cstrCancellationMessage ) = mrNo then
  begin
    ModalResult := mrNone;
    edtCategory.SetFocus;
    Exit;
  end;

  showmessage('Cancelling');

end;// procedure TfrmFeedingCategoryRecord.bbtCancelClick

//========================================================================================
procedure TfrmFeedingCategoryRecord.bbtHelpClick(Sender: TObject);
begin
showmessage('Help');
end;// procedure TfrmFeedingCategoryRecord.bbtHelpClick

//========================================================================================
//          CONTROL ROUTINES
//========================================================================================

//========== edtCategory =================================================================
procedure TfrmFeedingCategoryRecord.edtCategoryKeyPress(Sender: TObject;
  var Key: char);
begin
  Key := ValidAlphaCharacter(Key);
end;// procedure TfrmFeedingCategoryRecord.edtCategoryKeyPress

//========================================================================================
//          DATABASE ROUTINES
//========================================================================================

//========================================================================================
//          FORM ROUTINES
//========================================================================================
procedure TfrmFeedingCategoryRecord.FormCreate(Sender: TObject);
begin
  edtCategory.MaxLength := cbytMaxFeedingCategoryLength;
end;// procedure TfrmFeedingCategoryRecord.FormCreate

//========================================================================================
procedure TfrmFeedingCategoryRecord.FormShow(Sender: TObject);
begin
  edtCategory.SetFocus;
end;// procedure TfrmFeedingCategoryRecord.FormShow

//========================================================================================

end.// unit FeedingCategoryRecord;

